﻿using BusinessLayer;
using BusinessLayer.VDataEntities;
using CustomerServiceCenter.Models;
using CustomerServiceCenter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TimeServiceHelper
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            EventLog.WriteEntry("我的服务启动");//在系统事件查看器里的应用程序事件里来源的描述  
            writestr("服务启动");//自定义文本日志  
            System.Timers.Timer t = new System.Timers.Timer();
            //t.Interval = 10000;
            t.Interval = 1200000;
            t.Elapsed += new System.Timers.ElapsedEventHandler(ChkSrv);//到达时间的时候执行事件；   
            t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；   
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；   
        }

        /// <summary>  
        /// 定时检查，并执行方法  
        /// </summary>  
        /// <param name="source"></param>  
        /// <param name="e"></param>  
        public void ChkSrv(object source, System.Timers.ElapsedEventArgs e)
        {
            int intHour = e.SignalTime.Hour;
            int intMinute = e.SignalTime.Minute;
            int intSecond = e.SignalTime.Second;

            if (intHour >= 00 && intHour <= 01) ///定时设置,判断分时秒  
            {
                try
                {
                    System.Timers.Timer tt = (System.Timers.Timer)source;
                    tt.Enabled = false;
                    SetInnPoint();
                    tt.Enabled = true;
                }
                catch (Exception err)
                {
                    writestr(err.Message);
                }
            }
        }

        //我的方法  
        public void SetInnPoint()
        {
            try
            {
                writestr("服务运行");
                List<VTicketEntity> tickets = TicketBusiness.GetAllTickets();
                foreach (var item in tickets)
                {
                    //writestr(item.ToString());//自定义文本日志
                    if (null != item.ResolveDate)
                    {
                        TimeSpan span = new TimeSpan();
                        span = DateTime.Now - item.ResolveDate.Value;
                        if ((span.Days >= 3))
                        {
                            if (item.Status == 3)
                            {
                                ApplicationBusiness.MakeComments(Convert.ToInt32(item.TicketID), 5, "系统自动评价", Convert.ToInt32(TicketStatusType.Closed), Convert.ToInt32(TicketStatusType.Closed));
                                writestr("Ticket : " + item.TicketID + "系统已经自动评价");//自定义文本日志  
                                writestr("Ticket : " + item.TicketID + "ResolveDate: " + item.ResolveDate.Value);
                                writestr("Ticket : " + item.TicketID + "Days: " + span.Days.ToString());
                            }
                        }
                        else
                        {
                            writestr("Ticket : "+item.TicketID + " 暂时没有超过三天未评价");//自定义文本日志
                            writestr("Ticket : " + item.TicketID + "Days: " + span.Days.ToString());
                        }
                        
                    }
                    else
                    {
                        writestr(item.TicketID+"： 暂时没有未解决");//自定义文本日志
                    }
                }
                
                //这里执行你的东西  
                Thread.Sleep(10000);
            }
            catch (Exception err)
            {
                writestr(err.Message);
            }
        }

        ///在指定时间过后执行指定的表达式  
        ///  
        ///事件之间经过的时间（以毫秒为单位）  
        ///要执行的表达式  
        public static void SetTimeout(double interval, Action action)
        {
            System.Timers.Timer timer = new System.Timers.Timer(interval);
            timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
            {
                timer.Enabled = false;
                action();
            };
            timer.Enabled = true;
        }

        public void writestr(string readme)
        {
            //debug==================================================  
            //StreamWriter dout = new StreamWriter(@"c:\" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".txt");  
            StreamWriter dout = new StreamWriter(@"C:\" + "WServ_InnPointLog.txt", true);
            dout.Write("\r\n事件：" + readme + "\r\n操作时间：" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
            //debug==================================================  
            dout.Close();
        }

        protected override void OnStop()
        {
            writestr("服务停止");
            EventLog.WriteEntry("我的服务停止");
        }
    }
}
